/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.port;

import com.Ostermiller.util.ExcelCSVParser;
import com.Ostermiller.util.LabeledCSVParser;
import com.google.inject.Inject;
import org.apache.commons.lang.StringUtils;
import org.xptracker.domain.Customer;
import org.xptracker.domain.Story;
import org.xptracker.domain.User;
import org.xptracker.persistence.PersistenceManager;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class CsvImporter {
  @Inject
  private TypeFactory<Story> storyFields;

  @Inject
  private TypeFactory<User> userFields;

  public List<User> importUsers(InputStream inputStream) {
    List<User> results = new ArrayList<User>();
    for (Map<String, String> fieldToValues : getData(inputStream)) {
      User user = new User();
      results.add(user);
      for (String key : fieldToValues.keySet()) {
        final SimpleType<User> simpleType = userFields.makeType(key);
        simpleType.setImportValue(user, fieldToValues.get(key), null);
      }
    }
    return results;
  }

  public List<Story> importStories(InputStream inputStream, PersistenceManager.UpdateReceiver receiver) {
    List<Story> results = new ArrayList<Story>();
    for (Map<String, String> fieldToValues : getData(inputStream)) {
      Story story = new Story();
      results.add(story);
      for (String key : fieldToValues.keySet()) {
        final SimpleType<Story> simpleType = storyFields.makeType(key);
        simpleType.setImportValue(story, fieldToValues.get(key), receiver);
      }
    }
    return results;
  }


  private List<Map<String, String>> getData(InputStream inputStream) {
    try {
      LabeledCSVParser parser = new LabeledCSVParser(new ExcelCSVParser(inputStream));
      List<Map<String, String>> results = new ArrayList<Map<String, String>>();

      String[] labels = parser.getLabels();
      while (parser.getLine() != null) {
        Map<String, String> fieldToValue = new HashMap<String, String>();
        for (String label : labels) {
          String value =  parser.getValueByLabel(label);
          value = StringUtils.isNotBlank(value) ? value.trim() : value;
          fieldToValue.put(label, value);
        }
        results.add(fieldToValue);
      }
      parser.close();
      return results;
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public Collection<Customer> importCustomers(InputStream inputStream) throws RuntimeException {
    Collection<Customer> customerNames = new ArrayList<Customer>();
    List<Map<String, String>> data = getData(inputStream);
    for (Map<String, String> fieldToValues : data) {
      for (String key : fieldToValues.keySet()) {
        if (PortableFields.CUSTOMER_NAME.equals(key)) {
          String customerName = fieldToValues.get(key);
          if (StringUtils.isNotBlank(customerName)) {
            customerNames.add(new Customer(customerName.trim()));
          }
        }
      }
    }
    return customerNames;
  }
}
